﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DAL;

namespace BLL
{
    public class THONGTINKHACBLL
    {
        DataService DB = new DataService();

        public void insert(string noidung, int mabenhan, DateTime ngaytao, string file_patch)
        {
            string sql = "insert into thongtinkhac values (@noidung,@mabenhan,@ngaytao,@file_patch)";

            SqlParameter paraNoidung = new SqlParameter("noidung", noidung);
            SqlParameter paraMabenhan = new SqlParameter("mabenhan", mabenhan);
            SqlParameter paraNgaytao = new SqlParameter("ngaytao", ngaytao);
            SqlParameter paraFile = new SqlParameter("file_patch", file_patch);

            DB.ExecuteNonQuery(sql, paraNoidung, paraMabenhan, paraNgaytao, paraFile);
        }

        public void update(int matt, string noidung, int mabenhan, DateTime ngaytao, string file_patch)
        {
            string sql = "update thongtinkhac set noidung = N'" + noidung + ", mabenhan = '" + mabenhan.ToString() + "', ngaytao = '" + ngaytao.ToString() + "' , file_patch = '" + file_patch + "' where matt = " + matt.ToString();
            DB.ExecuteNonQuery(sql);
        }

        public void delete(int matt)
        {
            string sql = "delete from thongtinkhac where matt = " + matt.ToString();
            DB.ExecuteNonQuery(sql);
        }

        public THONGTINKHAC getThongtinkhacWithIDBaiviet(int idBaiviet)
        {
            string sql = "select ttk.matt, ttk.noidung, ttk.mabenhan, ttk.ngaytao, ttk.file_patch "
                         + " from baiviet bv,hosobenhan hsba, thongtinkhac ttk where bv.mabaiviet = " + idBaiviet.ToString();

            DataTable table = new DataTable();
            table = DB.ExecuteReader(sql);

            THONGTINKHAC ttk = new THONGTINKHAC();
            DataRow r = table.Rows[0];

            ttk.matt = Int32.Parse(r[0].ToString());
            ttk.noidung = r[1].ToString();
            ttk.mabenhan = Int32.Parse(r[2].ToString());
            ttk.ngaytao = Convert.ToDateTime(r[3].ToString());
            try { ttk.file_patch = r[4].ToString(); }
            catch { ttk.file_patch = ""; }

            return ttk;
        }

        public void updateThongtinkhacContentWithId(int idTtk, string ttk)
        {
            string sql = "update thongtinkhac set noidung = N'" + ttk + "' where matt = " + idTtk;
            DB.ExecuteNonQuery(sql);
        }
    }
}
